﻿Imports Arwani
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Drawing


Partial Class Backend_ManageJobCard
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("ADMIN_USER") IsNot Nothing) Then
            bindgrid()
        Else
            Response.Redirect("Default.aspx")

        End If

    End Sub

    Protected Sub bindgrid()
        '"WHERE (a.custcode = '" & Request.Cookies("Code").Value & "') and " & _
        Dim awrcon As New ArwaniController
        Dim str As String = "SELECT IDENTITY (int, 1, 1) AS [ID], a.Articleno, a.Serialno, a.invno as [Invoice No.], b.DELDATE as [Invoice Date] into #temptry " & _
            "FROM serviceaftersale a INNER JOIN INVOICEMASTER b ON a.invno = b.INVOICENO " & _
            "WHERE" & _
            "(len(ltrim(rtrim(a.q1invno))) <> 0 or len(ltrim(rtrim(a.q2invno))) <> 0 or len(ltrim(rtrim(a.q3invno))) <> 0 or len(ltrim(rtrim(a.q4invno))) <> 0 ) " & _
            "GROUP BY a.Articleno, a.serialno, a.invno, b.DELDATE order by b.deldate,a.articleno " & _
            "SELECT * FROM #Temptry ORDER BY [ID] DROP TABLE #Temptry "
        Dim dtTable As DataTable = awrcon.ExecuteQuery(str)
        If dtTable.Rows.Count > 0 Then
            gridViewMaster.DataSource = dtTable
            gridViewMaster.DataBind()

        End If
    End Sub


    Protected Sub gridViewMaster_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridViewMaster.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim awrcon1 As New ArwaniController
            Dim customerID As String = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ID"))
            Dim gridViewNested As GridView = DirectCast(e.Row.FindControl("nestedGridView"), GridView)
            Dim lblarticleNo As String = DirectCast(e.Row.FindControl("lblarticleNo"), Label).Text
            Dim lblSerialNo As String = DirectCast(e.Row.FindControl("lblSerialNo"), Label).Text
            'Dim strQuery As String = "select * from serviceaftersale where articleno = '" & Trim(lblarticleNo) & "' and serialno = '" & Trim(lblSerialNo) & "' and custcode = '" & Request.Cookies("Code").Value & "' and (len(ltrim(rtrim(q1invno))) <> 0 or len(ltrim(rtrim(q2invno))) <> 0 or len(ltrim(rtrim(q3invno))) <> 0 or len(ltrim(rtrim(q4invno))) <> 0 ) order by [year]"
            Dim strQuery As String = "select * from serviceaftersale where articleno = '" & Trim(lblarticleNo) & "' and serialno = '" & Trim(lblSerialNo) & "' and (len(ltrim(rtrim(q1invno))) <> 0 or len(ltrim(rtrim(q2invno))) <> 0 or len(ltrim(rtrim(q3invno))) <> 0 or len(ltrim(rtrim(q4invno))) <> 0 ) order by [year]"
            Dim dtTable1 As DataTable = awrcon1.ExecuteQuery(strQuery)
            Dim j As Integer
            Dim x As Integer
            Dim i As Integer
            Dim dt As New DataTable
            Dim maintable As New DataTable

            maintable.Columns.Add("invoiceno")
            maintable.Columns.Add("totalamt")
            maintable.Columns.Add("deldate")
            maintable.Columns.Add("jobcard")

            For j = 0 To dtTable1.Rows.Count - 1
                For x = 1 To 4
                    Dim SQLstr As String = "select invoiceno,totalamt,deldate,JobCard from servicemaster where invoiceno = '" & Trim(IIf(IsDBNull(dtTable1.Rows(j).Item("Q" & x & "Invno")), "", dtTable1.Rows(j).Item("Q" & x & "Invno"))) & "'"
                    dt = awrcon1.ExecuteQuery(SQLstr)
                    If dt.Rows.Count > 0 Then


                        For i = 0 To dt.Rows.Count - 1
                            Dim R As DataRow = maintable.NewRow
                            R("invoiceno") = dt.Rows(i)("invoiceno")
                            R("totalamt") = CDec(dt.Rows(i)("totalamt")).ToString("f2")
                            R("deldate") = CDate(dt.Rows(i)("deldate")).ToString("dd/MM/yyyy")
                            R("jobcard") = dt.Rows(i)("jobcard")
                            maintable.Rows.Add(R)

                        Next
                    End If
                Next

            Next
            gridViewNested.DataSource = maintable
            gridViewNested.DataBind()
        End If
    End Sub
    Protected Sub nestedGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
          
        End If
    End Sub


    
    Protected Function GetCheckedOutValue(checkedOut As String, invoicenumber As String) As String
        If checkedOut = "" Then
            Dim ret As String = "<a style='cursor: pointer;' onclick=" & """ basicPopup('" & invoicenumber & "');return false;" & """>Upload Job Card</a>"
            Return ret

        Else
            Dim ret As String = "<a style='cursor: pointer;' onclick=" & """ basicPopup('" & invoicenumber & "');return false;" & """>Uploaded</a><a href='../Invoices/" & checkedOut & "' target='_blank'>&nbsp;/&nbsp;view</a>"

            Return ret
        End If
    End Function


End Class
